package com.swordget.commons;

import com.swordget.conf.UserProperties;
import com.swordget.service.HandlerLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: David
 * @Date: Created in 15:57 2020/4/17
 * @Description: 文件处理公共类
 * @Modified By:
 * @Version: 0.0.1
 */
@Component
public class FileHandlerUtil {

    private UserProperties userProperties;

    @Autowired
    public FileHandlerUtil(UserProperties userProperties, HandlerLogService handlerLogService) {
        this.userProperties = userProperties;
    }

    private final static Logger log = LoggerFactory.getLogger(FileHandlerUtil.class);

    public File uploadFile(FileInputStream inputStream, String fileName, String charsetName, String loginId) {
        String uploadPath = userProperties.getUploadPath();
        File dis = new File(uploadPath);
        if (!dis.exists()) {
            dis.mkdirs();
        }
        //避免文件名重复覆盖之前上传的文件
        String newName = System.currentTimeMillis() + fileName;
        File uploadFile = new File(uploadPath + File.separator + newName);
        try (FileInputStream copyInputStream = inputStream;
             FileOutputStream fileOutputStream = new FileOutputStream(uploadFile)) {
            byte[] bytes = new byte[1024];
            int readByte = 0;
            while ((readByte = copyInputStream.read(bytes)) > 0) {
                fileOutputStream.write(bytes, 0, readByte);
            }
        } catch (Exception e) {
            log.error("上传文件过程中出现异常", e);
        }
        return uploadFile;
    }

    public File uploadFile(File file, String fileName, String charsetName, String loginId) {
        try {
            return uploadFile(new FileInputStream(file), fileName, charsetName, loginId);
        } catch (FileNotFoundException e) {
            log.error("上传文件过程中出现异常", e);
        }
        return null;
    }

    public File uploadFile(MultipartFile multipartFile, String charsetName, String loginId) {
        try {
            //id 和 chorme 在这里文件名称会有区别
            String name = multipartFile.getOriginalFilename();
            String suffix = name.substring(name.lastIndexOf("."));
            String fileName = new Date().getTime() + suffix;
            InputStream inputStream = multipartFile.getInputStream();
            FileInputStream fileInputStream = (FileInputStream) inputStream;
            return uploadFile(fileInputStream, fileName, charsetName, loginId);
        } catch (IOException e) {
            log.error("上传文件过程中出现异常", e);
        }
        return null;
    }


    public File writerErrorFile(String errorLog) throws IOException {
        String errorPath = userProperties.getErrorLogPath();
        File file = new File(errorPath);
        if (!file.exists() || file.isDirectory()) {
            file.mkdirs();
        }
        String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        File log = new File(file.getAbsolutePath() + File.separator + "ErrorMsg" + time + ".txt");
        try (FileWriter fileWriter = new FileWriter(log);) {
            fileWriter.write(errorLog);
            fileWriter.flush();
        }
        return log;
    }

}
